#include "StdAfx.h"

Texture* borderTex = 0;

Dialog::Dialog(ui32 x, ui32 y, ui32 nX, ui32 nY, std::string szTitle, bool close) : m_text("Fonts\\FrizQT__.ttf", "Friz Quadrata TT", 7, 15),
m_body("Fonts\\FrizQT__.ttf", "Friz Quadrata TT", 7, 15)
{
	m_closeBox = close;
	m_szTitle = szTitle;
	m_position.x = (float)x;
	m_position.y = (float)y;
	m_size.x = (float)nX * 32;
	m_size.y = (float)nY * 32;
	m_moveAble = false;
	m_moveLast.x = 0;
	m_moveLast.y = 0;

	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = m_position.x + 8;
	m_baseVertices[0].y = m_position.y + 8;
	m_baseVertices[0].u = 0.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = m_position.x + m_size.x - 8;
	m_baseVertices[1].y = m_position.y + 8;
	m_baseVertices[1].u = 1.0f;
	m_baseVertices[1].v = 0.0f;

	m_baseVertices[2].x = m_position.x + m_size.x - 8;
	m_baseVertices[2].y = m_position.y + m_size.y - 8;
	m_baseVertices[2].u = 1.0f;
	m_baseVertices[2].v = 1.0f;

	m_baseVertices[3].x = m_position.x + 8;
	m_baseVertices[3].y = m_position.y + m_size.y - 8;
	m_baseVertices[3].u = 0.0f;
	m_baseVertices[3].v = 1.0f;

	m_edgeVertices1[0].z = m_edgeVertices1[1].z = m_edgeVertices1[2].z = m_edgeVertices1[3].z = 0.0f;
	m_edgeVertices1[0].rhw = m_edgeVertices1[1].rhw = m_edgeVertices1[2].rhw = m_edgeVertices1[3].rhw = 1.0f;
	m_edgeVertices1[0].x = m_position.x;
	m_edgeVertices1[0].y = m_position.y;
	m_edgeVertices1[0].u = 191.0f / 256.0f;
	m_edgeVertices1[0].v = 0.0f;

	m_edgeVertices1[1].x = m_position.x + 32;
	m_edgeVertices1[1].y = m_position.y;
	m_edgeVertices1[1].u = 191.0f / 256.0f;
	m_edgeVertices1[1].v = 1.0f;

	m_edgeVertices1[2].x = m_position.x + 32;
	m_edgeVertices1[2].y = m_position.y + 32;
	m_edgeVertices1[2].u = 159.0f / 256.0f;
	m_edgeVertices1[2].v = 1.0f;

	m_edgeVertices1[3].x = m_position.x;
	m_edgeVertices1[3].y = m_position.y + 32;
	m_edgeVertices1[3].u = 159.0f / 256.0f;
	m_edgeVertices1[3].v = 0.0f;

	m_edgeVertices2[0].z = m_edgeVertices2[1].z = m_edgeVertices2[2].z = m_edgeVertices2[3].z = 0.0f;
	m_edgeVertices2[0].rhw = m_edgeVertices2[1].rhw = m_edgeVertices2[2].rhw = m_edgeVertices2[3].rhw = 1.0f;
	m_edgeVertices2[0].x = m_position.x;
	m_edgeVertices2[0].y = m_position.y + m_size.y - 32;
	m_edgeVertices2[0].u = 161.0f / 256.0f;
	m_edgeVertices2[0].v = 0.0f;

	m_edgeVertices2[1].x = m_position.x + 32;
	m_edgeVertices2[1].y = m_position.y + m_size.y - 32;
	m_edgeVertices2[1].u = 161.0f / 256.0f;
	m_edgeVertices2[1].v = 1.0f;

	m_edgeVertices2[2].x = m_position.x + 32;
	m_edgeVertices2[2].y = m_position.y + m_size.y;
	m_edgeVertices2[2].u = 129.0f / 256.0f;
	m_edgeVertices2[2].v = 1.0f;

	m_edgeVertices2[3].x = m_position.x;
	m_edgeVertices2[3].y = m_position.y + m_size.y;
	m_edgeVertices2[3].u = 129.0f / 256.0f;
	m_edgeVertices2[3].v = 0.0f;

	m_edgeVertices3[0].z = m_edgeVertices3[1].z = m_edgeVertices3[2].z = m_edgeVertices3[3].z = 0.0f;
	m_edgeVertices3[0].rhw = m_edgeVertices3[1].rhw = m_edgeVertices3[2].rhw = m_edgeVertices3[3].rhw = 1.0f;
	m_edgeVertices3[0].x = m_position.x + m_size.x - 32;
	m_edgeVertices3[0].y = m_position.y + m_size.y - 32;
	m_edgeVertices3[0].u = 225.0f / 256.0f;
	m_edgeVertices3[0].v = 0.0f;

	m_edgeVertices3[1].x = m_position.x + m_size.x;
	m_edgeVertices3[1].y = m_position.y + m_size.y - 32;
	m_edgeVertices3[1].u = 225.0f / 256.0f;
	m_edgeVertices3[1].v = 0.99f;

	m_edgeVertices3[2].x = m_position.x + m_size.x;
	m_edgeVertices3[2].y = m_position.y + m_size.y;
	m_edgeVertices3[2].u = 193.0f / 256.0f;
	m_edgeVertices3[2].v = 0.99f;

	m_edgeVertices3[3].x = m_position.x + m_size.x - 32;
	m_edgeVertices3[3].y = m_position.y + m_size.y;
	m_edgeVertices3[3].u = 193.0f / 256.0f;
	m_edgeVertices3[3].v = 0.0f;

	m_edgeVertices4[0].z = m_edgeVertices4[1].z = m_edgeVertices4[2].z = m_edgeVertices4[3].z = 0.0f;
	m_edgeVertices4[0].rhw = m_edgeVertices4[1].rhw = m_edgeVertices4[2].rhw = m_edgeVertices4[3].rhw = 1.0f;
	m_edgeVertices4[0].x = m_position.x + m_size.x - 32;
	m_edgeVertices4[0].y = m_position.y;
	m_edgeVertices4[0].u = 255.0f / 256.0f;
	m_edgeVertices4[0].v = 0.0f;

	m_edgeVertices4[1].x = m_position.x + m_size.x;
	m_edgeVertices4[1].y = m_position.y;
	m_edgeVertices4[1].u = 255.0f / 256.0f;
	m_edgeVertices4[1].v = 0.99f;

	m_edgeVertices4[2].x = m_position.x + m_size.x;
	m_edgeVertices4[2].y = m_position.y + 32;
	m_edgeVertices4[2].u = 223.0f / 256.0f;
	m_edgeVertices4[2].v = 0.99f;

	m_edgeVertices4[3].x = m_position.x + m_size.x - 32;
	m_edgeVertices4[3].y = m_position.y + 32;
	m_edgeVertices4[3].u = 223.0f / 256.0f;
	m_edgeVertices4[3].v = 0.0f;

	m_titleVertices[0].z = m_titleVertices[1].z = m_titleVertices[2].z = m_titleVertices[3].z = 0.0f;
	m_titleVertices[0].rhw = m_titleVertices[1].rhw = m_titleVertices[2].rhw = m_titleVertices[3].rhw = 1.0f;
	m_titleVertices[0].x = m_position.x + m_size.x / 2 - 65;
	m_titleVertices[0].y = m_position.y - 12;
	m_titleVertices[0].u = 60.0f / 256.0f;
	m_titleVertices[0].v = 0.0f;

	m_titleVertices[1].x = m_position.x + m_size.x / 2 + 65;
	m_titleVertices[1].y = m_position.y - 12;
	m_titleVertices[1].u = 196.0f / 256.0f;
	m_titleVertices[1].v = 0.0f;

	m_titleVertices[2].x = m_position.x + m_size.x / 2 + 65;
	m_titleVertices[2].y = m_position.y + 28;
	m_titleVertices[2].u = 196.0f / 256.0f;
	m_titleVertices[2].v = 40.0f / 64.0f;

	m_titleVertices[3].x = m_position.x + m_size.x / 2 - 65;
	m_titleVertices[3].y = m_position.y + 28;
	m_titleVertices[3].u = 60.0f / 256.0f;
	m_titleVertices[3].v = 40.0f / 64.0f;

	m_closeVertices[0].x = m_position.x + m_size.x - 37;
	m_closeVertices[1].x = m_position.x + m_size.x - 5;
	m_closeVertices[2].x = m_position.x + m_size.x - 5;
	m_closeVertices[3].x = m_position.x + m_size.x - 37;

	m_closeVertices[0].y = m_position.y + 5;
	m_closeVertices[1].y = m_position.y + 5;
	m_closeVertices[2].y = m_position.y + 37;
	m_closeVertices[3].y = m_position.y + 37;

	m_closeVertices[0].u = m_closeVertices[3].u = 0;
	m_closeVertices[1].u = m_closeVertices[2].u = 1;

	m_closeVertices[0].v = m_closeVertices[1].v = 0;
	m_closeVertices[2].v = m_closeVertices[3].v = 1;

	for(ui32 i = 1; i < nX - 1; ++i)
	{
		Vertex3FRHWT* pVertices = new Vertex3FRHWT[4];
		pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 0.0f;
		pVertices[0].rhw = pVertices[1].rhw = pVertices[2].rhw = pVertices[3].rhw = 1.0f;

		pVertices[0].x = m_position.x + i * 32;
		pVertices[0].y = m_position.y;
		pVertices[0].u = 63.0f / 256.0f;
		pVertices[0].v = 0.0f;

		pVertices[1].x = m_position.x + (i + 1) * 32;
		pVertices[1].y = m_position.y;
		pVertices[1].u = 63.0f / 256.0f;;
		pVertices[1].v = 1.0f;

		pVertices[2].x = m_position.x + (i + 1) * 32;
		pVertices[2].y = m_position.y + 15;
		pVertices[2].u = 48.65f / 256.0f;
		pVertices[2].v = 1.0f;

		pVertices[3].x = m_position.x + i * 32;
		pVertices[3].y = m_position.y + 15;
		pVertices[3].u = 48.65f / 256.0f;
		pVertices[3].v = 0.0f;

		m_boxVertices.push_back(pVertices);

		pVertices = new Vertex3FRHWT[4];
		pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 0.0f;
		pVertices[0].rhw = pVertices[1].rhw = pVertices[2].rhw = pVertices[3].rhw = 1.0f;

		pVertices[0].x = m_position.x + i * 32;
		pVertices[0].y = m_position.y + m_size.y - 15;
		pVertices[0].u = 16.0f / 256.0f;
		pVertices[0].v = 0.0f;

		pVertices[1].x = m_position.x + (i + 1) * 32;
		pVertices[1].y = m_position.y + m_size.y - 15;
		pVertices[1].u = 16.0f / 256.0f;;
		pVertices[1].v = 1.0f;

		pVertices[2].x = m_position.x + (i + 1) * 32;
		pVertices[2].y = m_position.y + m_size.y;
		pVertices[2].u = 1.0f / 256.0f;
		pVertices[2].v = 1.0f;

		pVertices[3].x = m_position.x + i * 32;
		pVertices[3].y = m_position.y + m_size.y;
		pVertices[3].u = 1.0f / 256.0f;
		pVertices[3].v = 0.0f;

		m_boxVertices.push_back(pVertices);
	}

	for(ui32 i = 1; i < nY - 1; ++i)
	{
		Vertex3FRHWT* pVertices = new Vertex3FRHWT[4];
		pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 0.0f;
		pVertices[0].rhw = pVertices[1].rhw = pVertices[2].rhw = pVertices[3].rhw = 1.0f;

		pVertices[0].x = m_position.x;
		pVertices[0].y = m_position.y + i * 32;
		pVertices[0].u = 64.0f / 256.0f;
		pVertices[0].v = 0.0f;

		pVertices[1].x = m_position.x + 15;
		pVertices[1].y = m_position.y + i * 32;
		pVertices[1].u = 79.0f / 256.0f;
		pVertices[1].v = 0.0f;

		pVertices[2].x = m_position.x + 15;
		pVertices[2].y = m_position.y + (i + 1) * 32;
		pVertices[2].u = 79.0f / 256.0f;
		pVertices[2].v = 1.0f;

		pVertices[3].x = m_position.x;
		pVertices[3].y = m_position.y + (i + 1) * 32;
		pVertices[3].u = 64.0f / 256.0f;
		pVertices[3].v = 1.0f;

		m_boxVertices.push_back(pVertices);

		pVertices = new Vertex3FRHWT[4];
		pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 0.0f;
		pVertices[0].rhw = pVertices[1].rhw = pVertices[2].rhw = pVertices[3].rhw = 1.0f;

		pVertices[0].x = m_position.x + m_size.x - 15;
		pVertices[0].y = m_position.y + i * 32;
		pVertices[0].u = 112.0f / 256.0f;
		pVertices[0].v = 0.0f;

		pVertices[1].x = m_position.x + m_size.x;
		pVertices[1].y = m_position.y + i * 32;
		pVertices[1].u = 127.0f / 256.0f;
		pVertices[1].v = 0.0f;

		pVertices[2].x = m_position.x + m_size.x;
		pVertices[2].y = m_position.y + (i + 1) * 32;
		pVertices[2].u = 127.0f / 256.0f;
		pVertices[2].v = 1.0f;

		pVertices[3].x = m_position.x + m_size.x - 15;
		pVertices[3].y = m_position.y + (i + 1) * 32;
		pVertices[3].u = 112.0f / 256.0f;
		pVertices[3].v = 1.0f;

		m_boxVertices.push_back(pVertices);
	}

	m_closeDown = false;
	m_isDialog = true;

	m_baseTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\DialogFrame\\UI-DialogBox-Background.blp");
	m_edgeTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\DialogFrame\\UI-DialogBox-Border.blp");
	m_titleTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\DialogFrame\\UI-DialogBox-Header.blp");
	m_closeTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up.blp");
	m_cloDownTexture = CTextureHandler::GetInstance()->GetTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down.blp");
}

void Dialog::Draw()
{
	if(!m_visible)
	{
		std::set<EditBox*>::iterator eitr = m_edits.begin();
		std::set<EditBox*>::iterator eend = m_edits.end();

		for( ; eitr != eend; ++eitr)
			(*eitr)->m_visible = false;
		return;
	}
	else
	{
		std::set<EditBox*>::iterator eitr = m_edits.begin();
		std::set<EditBox*>::iterator eend = m_edits.end();

		for( ; eitr != eend; ++eitr)
			(*eitr)->m_visible = true;
	}
	sD3D->SetFVF(VERTEX3FRHWT);
	sD3D->SetTexture(0, *m_baseTexture);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_baseVertices, sizeof(Vertex3FRHWT));
	sD3D->SetTexture(0, *m_edgeTexture);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_edgeVertices1, sizeof(Vertex3FRHWT));
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_edgeVertices2, sizeof(Vertex3FRHWT));
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_edgeVertices3, sizeof(Vertex3FRHWT));
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_edgeVertices4, sizeof(Vertex3FRHWT));
	std::vector<Vertex3FRHWT*>::iterator itr = m_boxVertices.begin();
	std::vector<Vertex3FRHWT*>::iterator end = m_boxVertices.end();

	for( ; itr != end; ++itr)
		sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, (*itr), sizeof(Vertex3FRHWT));
	sD3D->SetTexture(0, *m_titleTexture);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_titleVertices, sizeof(Vertex3FRHWT));
	ui32 textLen = m_text.GetTextLen(m_szTitle, true);
	ui32 posX = (ui32)(m_position.x + m_size.x / 2 - textLen / 2);
	m_text.DrawTextA(posX, ui32(m_position.y) + 2, m_text.GetTextLen(m_szTitle), m_text.GetFontHeight() + 3, m_szTitle);

	if(!m_closeDown)
		sD3D->SetTexture(0, *m_closeTexture);
	else
		sD3D->SetTexture(0, *m_cloDownTexture);
	if(m_closeBox)
		sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_closeVertices, sizeof(Vertex3FRHWT));

	std::set<UIButton<Dialog>*>::iterator itr1 = m_buttons.begin();
	std::set<UIButton<Dialog>*>::iterator end1 = m_buttons.end();

	for( ; itr1 != end1; ++itr1)
		(*itr1)->Draw();

	std::vector<DialogText*>::iterator tItr = m_bodyText.begin();
	std::vector<DialogText*>::iterator tEnd = m_bodyText.end();

	for( ; tItr != tEnd; ++tItr)
	{
		if((*tItr)->visible)
		{
			if((*tItr)->useUnicode == false)
				m_body.DrawTextA((ui32)(*tItr)->pos.x, (ui32)(*tItr)->pos.y, m_body.GetTextLen((*tItr)->szText), 30, (*tItr)->szText);
			else
				m_body.DrawTextW((ui32)(*tItr)->pos.x, (ui32)(*tItr)->pos.y, m_body.GetTextLen((*tItr)->szText), 30, (*tItr)->wszText);
		}
	}

	std::set<Checkbox<Dialog>*>::iterator cItr = m_checkBoxes.begin();
	std::set<Checkbox<Dialog>*>::iterator cEnd = m_checkBoxes.end();

	for( ; cItr != cEnd; ++cItr)
		(*cItr)->Draw();

	std::set<EditBox*>::iterator eitr = m_edits.begin();
	std::set<EditBox*>::iterator eend = m_edits.end();

	for( ; eitr != eend; ++eitr)
		(*eitr)->Draw();

	std::set<Scrollbar<Dialog>*>::iterator sitr = m_scrolls.begin();
	std::set<Scrollbar<Dialog>*>::iterator send = m_scrolls.end();

	for( ; sitr != send; ++sitr)
		(*sitr)->Draw();

	std::set<VScrollbar<Dialog>*>::iterator vsitr = m_vscrolls.begin();
	std::set<VScrollbar<Dialog>*>::iterator vsend = m_vscrolls.end();

	for( ; vsitr != vsend; ++vsitr)
		(*vsitr)->Draw();

	std::set<RadioButton<Dialog>*>::iterator ritr = m_radios.begin();
	std::set<RadioButton<Dialog>*>::iterator rend = m_radios.end();

	for( ; ritr != rend; ++ritr)
		(*ritr)->Draw();

	std::set<TextureQuad<Dialog>*>::iterator qitr = m_quads.begin();
	std::set<TextureQuad<Dialog>*>::iterator qend = m_quads.end();

	for( ; qitr != qend; ++qitr)
	{
		(*qitr)->Init();
		if((*qitr)->visible)
		{
			sD3D->SetFVF(VERTEX3FRHWT);
			if((*qitr)->texture)
				sD3D->SetTexture(0, *(*qitr)->texture);
			else
				continue;
			sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, (*qitr)->vertices, sizeof(Vertex3FRHWT));
			if((*qitr)->border)
			{
				sD3D->SetTexture(0, *borderTex);
				sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, (*qitr)->bvertices, sizeof(Vertex3FRHWT));
			}
		}
	}
}

bool Dialog::DialogMouseActivity(bool left, Vertex2F pos, bool up)
{
	if(!m_visible)
		return false;

	if(m_closeDown)
	{
		m_visible = false;
		m_closeDown = false;
	}

	if(!up && m_closeBox)
	{
		if(pos.x >= m_closeVertices[0].x && pos.x <= m_closeVertices[1].x &&
			pos.y >= m_closeVertices[0].y && pos.y <= m_closeVertices[2].y)
			m_closeDown = true;
	}

	std::set<UIButton<Dialog>*>::iterator itr = m_buttons.begin();
	std::set<UIButton<Dialog>*>::iterator end = m_buttons.end();

	for( ; itr != end; ++itr)
	{
		Vertex2F& s = (*itr)->m_size;
		Vertex2F& p = (*itr)->m_position;
		if(pos.x >= p.x && pos.y >= p.y && pos.x <= p.x + s.x && pos.y <= p.y + s.y && (*itr)->m_visible && (*itr)->m_enabled)
		{
			if(up)
			{
				(*itr)->m_state = 0;
				(*itr)->OnMouseClick();
				(*itr)->MouseActivity(left, pos, up);
			}
			else
			{
				(*itr)->m_state = 1;
				(*itr)->MouseActivity(left, pos, up);
			}
		}
		else if((*itr)->m_state)
		{
			(*itr)->m_state = 0;
			(*itr)->OnMouseClick();
			(*itr)->MouseActivity(left, pos, true);
		}
	}

	std::set<Checkbox<Dialog>*>::iterator citr = m_checkBoxes.begin();
	std::set<Checkbox<Dialog>*>::iterator cend = m_checkBoxes.end();

	for( ; citr != cend; ++citr)
	{
		Vertex2F& s = (*citr)->m_size;
		Vertex2F& p = (*citr)->m_position;
		if(pos.x >= p.x && pos.y >= p.y && pos.x <= p.x + s.x && pos.y <= p.y + s.y && (*citr)->m_visible && (*citr)->m_enabled)
		{
			if(up)
				(*citr)->MouseActivity(left, pos, up);
			else
				(*citr)->MouseActivity(left, pos, up);
		}
		else if(up)
			(*citr)->MouseActivity(left, pos, true);
	}

	std::set<Scrollbar<Dialog>*>::iterator sitr = m_scrolls.begin();
	std::set<Scrollbar<Dialog>*>::iterator send = m_scrolls.end();

	for( ; sitr != send; ++sitr)
	{
		Vertex2F& s = (*sitr)->m_size;
		Vertex2F& p = (*sitr)->m_position;
		if(pos.x >= p.x && pos.y >= p.y && pos.x <= p.x + s.x && pos.y <= p.y + s.y && (*sitr)->m_visible && (*sitr)->m_enabled)
		{
			if(up)
				(*sitr)->MouseActivity(left, pos, up);
			else
				(*sitr)->MouseActivity(left, pos, up);
		}
		else if(up)
			(*sitr)->MouseActivity(left, pos, true);
	}

	std::set<VScrollbar<Dialog>*>::iterator vsitr = m_vscrolls.begin();
	std::set<VScrollbar<Dialog>*>::iterator vsend = m_vscrolls.end();

	for( ; vsitr != vsend; ++vsitr)
	{
		Vertex2F& s = (*vsitr)->m_size;
		Vertex2F& p = (*vsitr)->m_position;
		if(pos.x >= p.x && pos.y >= p.y && pos.x <= p.x + s.x && pos.y <= p.y + s.y && (*vsitr)->m_visible && (*vsitr)->m_enabled)
		{
			if(up)
				(*vsitr)->MouseActivity(left, pos, up);
			else
				(*vsitr)->MouseActivity(left, pos, up);
		}
		else if(up)
			(*vsitr)->MouseActivity(left, pos, true);
	}

	std::set<RadioButton<Dialog>*>::iterator ritr = m_radios.begin();
	std::set<RadioButton<Dialog>*>::iterator rend = m_radios.end();

	for( ; ritr != rend; ++ritr)
	{
		Vertex2F& s = (*ritr)->m_size;
		Vertex2F& p = (*ritr)->m_position;
		if(pos.x >= p.x && pos.y >= p.y && pos.x <= p.x + s.x && pos.y <= p.y + s.y && (*ritr)->m_visible && (*ritr)->m_enabled)
		{
			if(up)
				(*ritr)->MouseActivity(left, pos, up);
			else
				(*ritr)->MouseActivity(left, pos, up);
		}
		else if(up)
			(*ritr)->MouseActivity(left, pos, true);
	}

	if(up)
	{
		std::set<TextureQuad<Dialog>*>::iterator qitr = m_quads.begin();
		std::set<TextureQuad<Dialog>*>::iterator qend = m_quads.end();

		for( ; qitr != qend; ++qitr)
		{
			float xmin = (*qitr)->vertices[0].x;
			float ymin = (*qitr)->vertices[0].y;
			float xmax = (*qitr)->vertices[2].x;
			float ymax = (*qitr)->vertices[2].y;

			if(pos.x >= xmin && pos.x <= xmax && pos.y >= ymin && pos.y <= ymax && (*qitr)->visible)
				((*qitr)->instance->*(*qitr)->OnClick)((*qitr));
		}
	}

	if(pos.x >= m_titleVertices[0].x && pos.x <= m_titleVertices[1].x && pos.y >= m_titleVertices[0].y && pos.y <= m_titleVertices[2].y && !up)
	{
		m_moveLast = pos;
		m_moveAble = true;
	}
	else if(up)
		m_moveAble = false;

	if(pos.x >= m_position.x && pos.x <= m_position.x + m_size.x && pos.y >= m_position.y && pos.y <= m_position.y + m_size.y)
		return true;
	else
		return false;
}

void Dialog::OnMouseMove(ui32 x, ui32 y, bool lDown, bool rDown)
{
	if(!m_visible)
		return;
	if(m_moveAble)
	{
		m_position.x += x - m_moveLast.x;
		m_position.y += y - m_moveLast.y;
		for(ui32 i = 0; i < 4; ++i)
		{
			std::vector<Vertex3FRHWT*>::iterator itr = m_boxVertices.begin();
			std::vector<Vertex3FRHWT*>::iterator end = m_boxVertices.end();

			for( ; itr != end; ++itr)
			{
				(*itr)[i].x += x - m_moveLast.x;
				(*itr)[i].y += y - m_moveLast.y;
			}

			m_baseVertices[i].x += x - m_moveLast.x;
			m_baseVertices[i].y += y - m_moveLast.y;
			m_edgeVertices1[i].x += x - m_moveLast.x;
			m_edgeVertices1[i].y += y - m_moveLast.y;
			m_edgeVertices2[i].x += x - m_moveLast.x;
			m_edgeVertices2[i].y += y - m_moveLast.y;
			m_edgeVertices3[i].x += x - m_moveLast.x;
			m_edgeVertices3[i].y += y - m_moveLast.y;
			m_edgeVertices4[i].x += x - m_moveLast.x;
			m_edgeVertices4[i].y += y - m_moveLast.y;
			m_titleVertices[i].x += x - m_moveLast.x;
			m_titleVertices[i].y += y - m_moveLast.y;
			m_closeVertices[i].x += x - m_moveLast.x;
			m_closeVertices[i].y += y - m_moveLast.y;
		}
		std::vector<DialogText*>::iterator tItr = m_bodyText.begin();
		std::vector<DialogText*>::iterator tEnd = m_bodyText.end();

		for( ; tItr != tEnd; ++tItr)
		{
			(*tItr)->pos.x += x - m_moveLast.x;
			(*tItr)->pos.y += y - m_moveLast.y;
		}

		std::set<UIButton<Dialog>*>::iterator bItr = m_buttons.begin();
		std::set<UIButton<Dialog>*>::iterator bEnd = m_buttons.end();

		for( ; bItr != bEnd; ++bItr)
			(*bItr)->ModPosition(ui32(x - m_moveLast.x), ui32(y - m_moveLast.y));

		std::set<Checkbox<Dialog>*>::iterator cItr = m_checkBoxes.begin();
		std::set<Checkbox<Dialog>*>::iterator cEnd = m_checkBoxes.end();

		for( ; cItr != cEnd; ++cItr)
			(*cItr)->ModPosition(ui32(x - m_moveLast.x), ui32(y - m_moveLast.y));

		std::set<EditBox*>::iterator eItr = m_edits.begin();
		std::set<EditBox*>::iterator eEnd = m_edits.end();

		for( ; eItr != eEnd; ++eItr)
			(*eItr)->ModPosition(ui32(x - m_moveLast.x), ui32(y - m_moveLast.y));

		std::set<Scrollbar<Dialog>*>::iterator sitr = m_scrolls.begin();
		std::set<Scrollbar<Dialog>*>::iterator send = m_scrolls.end();

		for( ; sitr != send; ++sitr)
			(*sitr)->ModPosition(i32(x - m_moveLast.x), i32(y - m_moveLast.y));

		std::set<VScrollbar<Dialog>*>::iterator vsitr = m_vscrolls.begin();
		std::set<VScrollbar<Dialog>*>::iterator vsend = m_vscrolls.end();

		for( ; vsitr != vsend; ++vsitr)
			(*vsitr)->ModPosition(i32(x - m_moveLast.x), i32(y - m_moveLast.y));

		std::set<RadioButton<Dialog>*>::iterator ritr = m_radios.begin();
		std::set<RadioButton<Dialog>*>::iterator rend = m_radios.end();

		for( ; ritr != rend; ++ritr)
			(*ritr)->ModPosition(i32(x - m_moveLast.x), i32(y - m_moveLast.y));

		std::set<TextureQuad<Dialog>*>::iterator qitr = m_quads.begin();
		std::set<TextureQuad<Dialog>*>::iterator qend = m_quads.end();

		for( ; qitr != qend; ++qitr)
		{
			for(ui32 i = 0; i < 4; ++i)
			{
				(*qitr)->vertices[i].x += x - m_moveLast.x;
				(*qitr)->vertices[i].y += y - m_moveLast.y;
				(*qitr)->bvertices[i].x += x - m_moveLast.x;
				(*qitr)->bvertices[i].y += y - m_moveLast.y;
			}
		}

		m_moveLast.x = (float)x;
		m_moveLast.y = (float)y;
	}
	std::set<Scrollbar<Dialog>*>::iterator sitr = m_scrolls.begin();
	std::set<Scrollbar<Dialog>*>::iterator send = m_scrolls.end();

	for( ; sitr != send; ++sitr)
		(*sitr)->OnMouseMove(x, y, lDown, rDown);

	std::set<VScrollbar<Dialog>*>::iterator vsitr = m_vscrolls.begin();
	std::set<VScrollbar<Dialog>*>::iterator vsend = m_vscrolls.end();

	for( ; vsitr != vsend; ++vsitr)
		(*vsitr)->OnMouseMove(x, y, lDown, rDown);

	std::for_each(m_checkBoxes.begin(), m_checkBoxes.end(), 
		[x, y, lDown, rDown](Checkbox<Dialog>* cb) { cb->OnMouseMove(x, y, lDown, rDown); });
}